

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class tableroBD {
	public static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";
	public static final String CONNECTION_URL = "jdbc:sqlite:bd/Ajedrez.s3db";
	/**
	 * @param args
	 */
	
	public static void inicializarTablero() throws SQLException, ClassNotFoundException  {
		Class.forName(DRIVER_CLASS_NAME);
		Connection connection = DriverManager.getConnection(CONNECTION_URL);
		borrarTablero();
				String sqlStatementString = null;
				Statement statement = null;
				statement = connection.createStatement();
				
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+1+"','torre')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+2+"','caballo')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+3+"','alfil')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+4+"','rey')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+5+"','reina')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+6+"','alfil')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+7+"','caballo')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+1+"','"+8+"','torre')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+1+"','torre')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+2+"','caballo')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+3+"','alfil')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+4+"','rey')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+5+"','reina')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+6+"','alfil')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+7+"','caballo')";
				statement.executeUpdate(sqlStatementString);
				sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+8+"','"+8+"','torre')";
				statement.executeUpdate(sqlStatementString);
				
				for (int u=1;u<=8;u++){
					sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+2+"','"+u+"','peon')";
					statement.executeUpdate(sqlStatementString);
					sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+7+"','"+u+"','peon')";
					statement.executeUpdate(sqlStatementString);
				}
				
				
				for(int i=3;i<=6;i++){
					for (int j=1;j<=8;j++){
						sqlStatementString = "INSERT INTO tablero(fila, columna, ficha) VALUES ('"+i+"','"+j+"','null')";
						statement.executeUpdate(sqlStatementString);
					}
				}
				
				statement.close();
			}

	
	
	public static void borrarTablero() throws SQLException, ClassNotFoundException  {
		Class.forName(DRIVER_CLASS_NAME);
		Connection connection = DriverManager.getConnection(CONNECTION_URL);
	
				String sqlStatementString = null;
				Statement statement = null;
				statement = connection.createStatement();
				
				
				sqlStatementString = "DELETE FROM tablero";
				statement.executeUpdate(sqlStatementString);
				
				
				
				statement.close();
			}
	
	public static void moverPieza(int fila, int col, int filan, int coln) throws SQLException, ClassNotFoundException  {
		Class.forName(DRIVER_CLASS_NAME);
		Connection connection = DriverManager.getConnection(CONNECTION_URL);
		
		String sqlStatementString = null;
		Statement statement = null;
		ResultSet resultSet = null;
		statement = connection.createStatement();
		sqlStatementString = "SELECT * FROM tablero WHERE fila='"+fila+"' AND columna='"+col+"' ";
		resultSet = statement.executeQuery(sqlStatementString);
		String ficha = resultSet.getString("ficha");
		statement = connection.createStatement();
		sqlStatementString = "UPDATE tablero SET ficha='"+ficha+"' WHERE fila='"+filan+"' AND columna='"+coln+"' ";
		statement.executeUpdate(sqlStatementString);
		sqlStatementString = "UPDATE tablero SET ficha='"+null+"' WHERE fila='"+fila+"' AND columna='"+col+"' ";
		statement.executeUpdate(sqlStatementString);
		statement.close();
		resultSet.close();
	}
	
	
	//metodo para devolver un array de casillas.
	//public static Casilla [][] arraytablero(){}

	
	
	
	
	
	
	
	public static void main(String[] args) throws SQLException, ClassNotFoundException {
		// TODO Auto-generated method stub
		inicializarTablero();
	}

}
